package com.kly.cashmall.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;

/**
 * 简化ViewHolder
 */
public class ViewHolder {

  private SparseArray<View> viewMap = new SparseArray<>();
  public View itemView;

  /**
   * @param view
   */
  private ViewHolder(View view) {
    if (view != null) {
      this.itemView = view;
      view.setTag(this);
    }
  }

  public void clear() {
    viewMap.clear();
    itemView = null;
  }

  public static ViewHolder getHolder(@Nullable Activity activity) {
    if (activity == null) {
      return new ViewHolder(null);
    }
    return getHolder(activity.findViewById(android.R.id.content));
  }

  public static ViewHolder getHolder(@Nullable Fragment fragment) {
    if (fragment == null) {
      return new ViewHolder(null);
    }
    return getHolder(fragment.getView());
  }

  /**
   * 获取实例的同时在view中缓存住本身
   */
  public static ViewHolder getHolder(@Nullable View view) {

    if (view == null) {
      return new ViewHolder(null);
    }

    Object obj = view.getTag();

    if (obj != null && obj instanceof ViewHolder) {
      return (ViewHolder) obj;
    }
    return new ViewHolder(view);
  }

  public <T extends View> T get(@IdRes int id) {
    View childView = viewMap.get(id);
    if (childView == null && itemView != null) {
      childView = itemView.findViewById(id);
      viewMap.put(id, childView);
    }
    return (T) childView;
  }

  public <T extends View> T getView(@IdRes int... ids) {
    if (ids.length == 0) {
      return null;
    }
    View view = get(ids[0]);
    for (int i = 1; i < ids.length; i++) {
      if (view != null) {
        view = view.findViewById(ids[i]);
      }
    }
    return (T) view;
  }

  public ViewHolder setText(@IdRes int id, CharSequence text) {
    TextView tv = get(id);
    if (tv != null) {
      tv.setText(text);
    }
    return this;
  }

  public ViewHolder setText(@IdRes int id, @StringRes int strId) {
    TextView tv = get(id);
    if (tv != null) {
      if (strId == 0) {
        tv.setText("");
      } else {
        tv.setText(strId);
      }
    }
    return this;
  }

  public ViewHolder setText(View textView, CharSequence text) {
    if (textView instanceof TextView) {
      ((TextView) textView).setText(text);
    }
    return this;
  }

  public ViewHolder setTextColor(@IdRes int id, int color) {
    TextView tv = get(id);
    if (tv != null) {
      tv.setTextColor(color);
    }
    return this;
  }

  public ViewHolder setTextSize(@IdRes int id, int sp) {
    TextView tv = get(id);
    if (tv != null) {
      tv.setTextSize(sp);
    }
    return this;
  }

  public ViewHolder setImageResource(@IdRes int id, @DrawableRes int drawId) {
    ImageView iv = get(id);
    if (iv != null) {
      iv.setImageResource(drawId);
    }
    return this;
  }

  public ViewHolder setVisible(@IdRes int id, boolean visible) {
    View tv = get(id);
    if (tv != null) {
      tv.setVisibility(visible ? View.VISIBLE : View.GONE);
    }
    return this;
  }

  public ViewHolder setEnable(@IdRes int id, boolean enable) {
    View tv = get(id);
    if (tv != null) {
      tv.setEnabled(enable);
    }
    return this;
  }

  public ViewHolder setSelected(@IdRes int id, boolean enable) {
    View tv = get(id);
    if (tv != null) {
      tv.setSelected(enable);
    }
    return this;
  }

  public ViewHolder setClickListener(View.OnClickListener listener) {
    return setDelayClickListener(listener, 800);
  }

  public ViewHolder setDelayClickListener(View.OnClickListener listener, int delay) {
    if (itemView != null) {
      itemView.setOnClickListener(listener);
    }
    return this;
  }

  public ViewHolder setClickListener(@IdRes int id, View.OnClickListener listener) {
    return setDelayClickListener(id, listener, 800);
  }

  public ViewHolder setDelayClickListener(@IdRes int id, View.OnClickListener listener, int delay) {
    View tv = get(id);
    if (tv != null) {
      tv.setOnClickListener(listener);
    }
    return this;
  }

  public ViewHolder setChildClickListener(@IdRes int id, @IdRes int childId,
                                          View.OnClickListener listener) {
    View tv = get(id);
    if (tv != null) {
      View child = tv.findViewById(childId);
      if (child != null) {
        child.setOnClickListener(listener);
      }
    }
    return this;
  }

  public ViewHolder setLongClickListener(View.OnLongClickListener listener) {
    if (itemView != null) {
      itemView.setOnLongClickListener(listener);
    }
    return this;
  }

  public ViewHolder setLongClickListener(@IdRes int id, View.OnLongClickListener listener) {
    View tv = get(id);
    if (tv != null) {
      tv.setOnLongClickListener(listener);
    }
    return this;
  }

  public ViewHolder loadImage(@IdRes int id, String imageUrl) {
    ImageView iv = get(id);
    if (iv != null) {
//      ImageLoader.getInstance().displayImage(iv, imageUrl);
    }
    return this;
  }

  public ViewHolder loadImage(@IdRes int id, Drawable drawable) {
    ImageView iv = get(id);
    if (iv != null) {
      iv.setImageDrawable(drawable);
    }
    return this;
  }

  public ViewHolder loadImage(Context context, @IdRes int id, String url) {
    ImageView iv = get(id);
    if (iv != null) {
      Glide.with(context).load(url).into(iv);
    }
    return this;
  }
}